package cn.virens.server.netty_udp;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.virens.common.BytesBuilder;
import cn.virens.server.netty_udp.packet.UdpWritePacket;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.DatagramPacketEncoder;
import io.netty.handler.codec.MessageToMessageEncoder;

@SuppressWarnings("unused")
public class UDPServerPacketEncoder extends MessageToMessageEncoder<UdpWritePacket> {
	private Logger logger = LoggerFactory.getLogger(DatagramPacketEncoder.class);

	private final UDPServerPacketHandler uDPServerPacketHandler;

	public UDPServerPacketEncoder(UDPServerPacketHandler uDPServerPacketHandler) {
		this.uDPServerPacketHandler = uDPServerPacketHandler;
	}

	@Override
	protected void encode(ChannelHandlerContext ctx, UdpWritePacket packet, List<Object> out) throws Exception {
		// 用户数据区 ----------------------------------START
		BytesBuilder userData = new BytesBuilder();
		userData.append(packet.seqByte());
		userData.append(packet.lenByte());
		userData.append(packet.cmdByte());
		userData.append(packet.dataByte());

		// 发送数据
		out.add(packet.wrap(userData.toArray()));
	}
}
